package com.xwx.common.utils;
/**
 * 
* @Description:SQL语句辅助类,提供各种sql拼接常用功能方法
* @author:Dream QQ:68697015
* @time:2015年3月25日 下午2:54:11
 */
public class Condition {
	
	protected static final String EQUAL = " = ";
	protected static final String NO_EQUAL = " <> ";
	protected static final String LT = " < ";
	protected static final String LE = " <= ";
	protected static final String GE = " >= ";
	protected static final String GT = " > ";
	protected static final String BETWEEN = " between ";
	protected static final String NO_BETWEEN = " not between ";
	protected static final String IS_NULL = " is null";
	protected static final String NOT_NULL = " is not null";
	protected static final String AND=" and ";
	protected static final String WHERE_STR = " where 1=1 ";
	
	private StringBuffer sqlStr = null;
	//初始化
	public Condition(){
		sqlStr = new StringBuffer(WHERE_STR);
	}
	/**
	 * 字段值为空
	 * @param param
	 * @return
	 */
	public Condition isNull(String param){
		sqlStr.append(AND).append(param).append(IS_NULL);
		return this;
	}
	/**
	 * 字段值非空
	 * @param param
	 * @return
	 */
	public Condition isNotNull(String param){
		sqlStr.append(AND).append(param).append(NOT_NULL);
		return this;		
	}
	/**
	 * 字段值不等于给定值,默认以字符串处理
	 * @param param 字段
	 * @param value 值
	 * @return
	 */
	public Condition ne(String param, Object value){
		sqlStr.append(AND).append(param).append(NO_EQUAL).append("'").append(value).append("'");
		return this;
	}
	/**
	 * 字段值不等于给定值,数类字类型
	 * @param param 字段
	 * @param value 值
	 * @return
	 */
	public Condition neInt(String param, Object value){
		sqlStr.append(AND).append(param).append(NO_EQUAL).append(value);
		return this;
	}	
	/**
	 * 字段值等于给定值,默认以字符串处理
	 * @param param
	 * @param value
	 * @return
	 */
	public Condition eq(String param, Object value){
		sqlStr.append(AND).append(param).append(EQUAL).append("'").append(value).append("'");
		return this;
	}
	/**
	 * 字段值等于给定值,数字类型
	 * @param param
	 * @param value
	 * @return
	 */
	public Condition eqInt(String param, Object value){
		sqlStr.append(AND).append(param).append(EQUAL).append(value);
		return this;
	}	
	
	/**
	 * 字段值小于给定值
	 * @param param
	 * @param value
	 * @return
	 */
	public Condition lt(String param, Object value){
		sqlStr.append(AND).append(param).append(LT).append(value);
		return this;
	}
	/**
	 * 字段值小于或者等于给定值
	 * @param param
	 * @param value
	 * @return
	 */
	public Condition le(String param, Object value){
		sqlStr.append(AND).append(param).append(LE).append(value);
		return this;
	}	
	/**
	 * 字段值大于给定值
	 * @param param
	 * @param value
	 * @return
	 */
	public Condition gt(String param, Object value){
		sqlStr.append(AND).append(param).append(GT).append(value);
		return this;
	}	
	/**
	 * 字段值大于或者等于给定值
	 * @param param
	 * @param value
	 * @return
	 */
	public Condition ge(String param, Object value){
		sqlStr.append(AND).append(param).append(GE).append(value);
		return this;
	}
	/**
	 * 字段值在某范围内
	 * @param param 字段名
	 * @param firstVal 范围的起始
	 * @param secondVal 范围的结束
	 * @return
	 */
	public Condition between(String param, Object firstVal,Object secondVal){
		sqlStr.append(AND).append(param).append(BETWEEN).append(firstVal)
		.append(AND).append(secondVal);
		return this;
	}	
	/**
	 * 字段值不在某范围内
	 * @param param 字段名
	 * @param firstVal 范围的起始
	 * @param secondVal 范围的结束
	 * @return
	 */
	public Condition nb(String param, Object firstVal,Object secondVal){
		sqlStr.append(AND).append(param).append(NO_BETWEEN).append(firstVal)
		.append(AND).append(secondVal);
		return this;
	}
	/**
	 * 全模糊匹配
	 * @param param
	 * @param value
	 * @return
	 */
	public Condition like(String param,Object value){
		sqlStr.append(AND).append(param).append(" like %").append(value).append("%");
		return this;
	}
	/**
	 * 左模糊匹配
	 * @param param
	 * @param value
	 * @return
	 */
	public Condition llike(String param,Object value){
		sqlStr.append(AND).append(param).append(" like %").append(value);
		return this;
	}
	/**
	 * 右模糊匹配
	 * @param param
	 * @param value
	 * @return
	 */
	public Condition rlike(String param,Object value){
		sqlStr.append(AND).append(param).append(" like ").append(value).append("%");
		return this;
	}
	/**
	 * in查询
	 * @param param
	 * @param value
	 * @return
	 */
	public Condition in(String param,Object...value){
		sqlStr.append(AND).append(param).append(" in(").append(value).append("");
		return this;
	}
	/**
	 * not in查询
	 * @param param
	 * @param value
	 * @return
	 */	
	public Condition notIn(String param,Object...value){
		sqlStr.append(AND).append(param).append(" not in(").append(value).append("");
		return this;
	}
	public void clear(){
		sqlStr=new StringBuffer(WHERE_STR);
	}
	public String toString(){
		return sqlStr.toString();
	}
	
	public static void main(String[] args) {
		
	}
}
